package com.wyvoid.forum.web.action.message;

import com.wyvoid.forum.web.action.BaseAction;
import com.wyvoid.forum.domain.Message;
import com.wyvoid.forum.domain.User;
import com.wyvoid.forum.domain.SessionUser;
import com.wyvoid.forum.service.IMessageService;
import com.wyvoid.forum.service.IUserService;
import com.wyvoid.forum.annotations.Secure;
import com.wyvoid.core.persistent.Pagination;
import com.wyvoid.core.persistent.hibernate.HibernatePage;
import net.sourceforge.stripes.action.*;
import net.sourceforge.stripes.integration.spring.SpringBean;
import net.sourceforge.stripes.validation.ValidateNestedProperties;
import net.sourceforge.stripes.validation.Validate;
import org.apache.commons.lang.StringUtils;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * Created by IntelliJ IDEA.
 * User: new
 * Date: 2007-5-18
 * Time: 9:28:41
 * To change this template use File | Settings | File Templates.
 */
@UrlBinding("/message/message.do")
@Secure
public class MessageAction extends BaseAction {
    @ValidateNestedProperties({
    @Validate(field = "title", required = true,  maxlength = 128),
    @Validate(field = "content", required = true,  maxlength = 65535)
            })

    private Message message;
    private IUserService userService;
    private IMessageService messageService;
    private HibernatePage myMessageList;
    private HibernatePage draftMessageList;
    private HibernatePage trashMessagesList;
    private HibernatePage mySendMessageList;
    private long messageId;
    private long receiveUserId;
    private int type;


    public void setMessage(Message message) {
        this.message = message;
    }

    public Message getMessage() {
        return message;
    }

    @SpringBean
    public void setUserService(IUserService userService) {
        this.userService = userService;
    }
    @SpringBean
    public void setMessageService(IMessageService IMessageService) {
        this.messageService = IMessageService;
    }

    public HibernatePage getMyMessageList() {
        return myMessageList;
    }


    public HibernatePage getDraftMessageList() {
        return draftMessageList;
    }

    public HibernatePage getTrashMessagesList() {
        return trashMessagesList;
    }


    public HibernatePage getMySendMessageList() {
        return mySendMessageList;
    }

    public long getMessageId() {
        return messageId;
    }

    public void setMessageId(long messageId) {
        this.messageId = messageId;
    }


    public long getReceiveUserId() {
        return receiveUserId;
    }

    public void setReceiveUserId(long receiveUserId) {
        this.receiveUserId = receiveUserId;
    }

    @DontValidate
    public Resolution writeMessageForm() {
        if (receiveUserId!=0) {
            User receiveUser =userService.getUser(receiveUserId);
            context.getRequest().setAttribute("receiveUser",receiveUser);
            return getBBSResolution("message/writeMessageForm");
        }else{
            //无接收消息者操作
            return null;
        }
    }
    //写消息

     public Resolution writeMessage() {
       SessionUser sessionUser=getContext().getUser();
       message.setSendUserId(sessionUser.getId());
       message.setSendNickName(sessionUser.getNickname());
       message.setCreateTime(new Date());
       
       messageService.createMessage(message);
       return getBBSResolution("message/success");
      }

    //继续撰写草稿
    @DontValidate
    public Resolution updateMessageForm(){

        SessionUser sessionUser=getContext().getUser();
           message=messageService.getMessage(messageId);
           return getBBSResolution("message/updateMessageForm");
    }


    public Resolution updateMessage(){
        SessionUser sessionUser=getContext().getUser();
        Message updateMessage=messageService.getMessage(message.getId());
            updateMessage.setCreateTime(new Date());
            updateMessage.setTitle(message.getTitle());
            updateMessage.setContent(message.getContent());
            updateMessage.setType(message.getType());
            messageService.editMessage(updateMessage);
            return getBBSResolution("message/success");
    }

    //消息放入回收站
    @DontValidate
    public Resolution setTrashMessage(){
        Message message=messageService.getMessage(messageId);
        SessionUser sessionUser=getContext().getUser();
            if(message.getSendUserId()==sessionUser.getId()){
                message.setDeleteBySender(true);
            }else if(message.getReceiveUserId()==sessionUser.getId()){
                message.setDeleteByReceiver(true);
            }
            messageService.editMessage(message);
           /* return getBBSResolution("message/success");*/
            String targetUrl = context.getRequest().getHeader("referer");
            if (StringUtils.isNotBlank(targetUrl)) {
                return new RedirectResolution(targetUrl, false);
            } else {
                return new RedirectResolution("/");
            }
    }
    
    //彻底删除
    @DontValidate
    public Resolution deleteMessage(){
        SessionUser sessionUser=getContext().getUser();

            messageService.deleteMessage(messageId);
             String targetUrl = context.getRequest().getHeader("referer");
            if (StringUtils.isNotBlank(targetUrl)) {
                return new RedirectResolution(targetUrl, false);
            } else {
                return new RedirectResolution("/");
            }
           /* return getBBSResolution("message/success");*/
    }

    //我的收信箱
    @DontValidate
    @DefaultHandler
    public Resolution myMessage(){
        SessionUser sessionUser = getContext().getUser();

        Pagination pagination=new Pagination();    
        pagination.setPageNumber(1);
        pagination.setPageSize(10);
        myMessageList = messageService.getMyMessages(sessionUser.getId(), pagination);
        return getBBSResolution("message/myMessage");
    }

    //我的发信箱
    @DontValidate
    public Resolution mySendMessage(){
        SessionUser sessionUser=getContext().getUser();

            Pagination pagination=new Pagination();
            pagination.setPageNumber(1);
            pagination.setPageSize(10);
            mySendMessageList=messageService.getMySendMessages(sessionUser.getId(),pagination);
            return getBBSResolution("message/mySendMessage"); 
    }

    //草稿箱
    @DontValidate
    public Resolution draftMessage(){
        SessionUser sessionUser=getContext().getUser();
            Pagination pagination=new Pagination();
            pagination.setPageNumber(1);
            pagination.setPageSize(10);
            draftMessageList=messageService.getDraftMessages(-1,sessionUser.getId(),pagination);
            return getBBSResolution("message/draftMessage");
      }
    //费件箱
   @DontValidate
    public Resolution trashMessages(){
        SessionUser sessionUser=getContext().getUser();
             Pagination pagination=new Pagination();
            pagination.setPageNumber(1);
            pagination.setPageSize(10);
            trashMessagesList=messageService.getTrashMessages(sessionUser.getId(),pagination);
            return getBBSResolution("message/trashMessage");
      }

    //阅读消息
    @DontValidate
    public Resolution readMessage(){
        SessionUser sessionUser=getContext().getUser();
        message=messageService.getMessage(messageId);
            message.setReaded(true);
            messageService.editMessage(message);
            return getBBSResolution("message/message");
    }





}
